package cn.fxzhang.leetcode.contest.week236;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MKAverage {

    private int m;
    private int k;

    private List<Integer> list = new ArrayList<>();

    public MKAverage(int m, int k) {
        this.m = m;
        this.k = k;
    }

    public void addElement(int num) {
        list.add(num);
        if (list.size() > m){
            list.remove(0);
        }
    }

    public int calculateMKAverage() {
        if (list.size() < m){
            return -1;
        }
        List<Integer> copy = new ArrayList<>(list);
        Collections.sort(copy);
        List<Integer> subList = copy.subList(k, m - k);
        long l = 0;
        for (Integer num : subList) {
            l = l+num;
        }
        return (int)(l/subList.size());
    }
}
